/**
 * Created by evil on 16-11-10.
 */
var testEditor;
var release_newpost = {

    editormd: function (md_path)
    {
        if (md_path == null)
            md_path = "/static/other/test.md";
        $.get(md_path, function (md)
        {
            testEditor = editormd("editormd", {
                width: "90%",
                height: 640,
                path: 'https://pandao.github.io/editor.md/lib/',
                theme: "dark",
                previewTheme: "dark",
                editorTheme: "pastel-on-dark",
                markdown: md,
                codeFold: true,
                //syncScrolling : false,
                saveHTMLToTextarea: true,    // 保存 HTML 到 Textarea
                searchReplace: true,
                //watch : false,                // 关闭实时预览
                htmlDecode: "style,script,iframe|on*",            // 开启 HTML 标签解析，为了安全性，默认不开启
                //toolbar  : false,             //关闭工具栏
                //previewCodeHighlight : false, // 关闭预览 HTML 的代码块高亮，默认开启
                emoji: true,
                taskList: true,
                tocm: true,         // Using [TOCM]
                tex: true,                   // 开启科学公式TeX语言支持，默认关闭
                flowChart: true,             // 开启流程图支持，默认关闭
                sequenceDiagram: true,       // 开启时序/序列图支持，默认关闭,
                //dialogLockScreen : false,   // 设置弹出层对话框不锁屏，全局通用，默认为true
                //dialogShowMask : false,     // 设置弹出层对话框显示透明遮罩层，全局通用，默认为true
                //dialogDraggable : false,    // 设置弹出层对话框不可拖动，全局通用，默认为true
                //dialogMaskOpacity : 0.4,    // 设置透明遮罩层的透明度，全局通用，默认值为0.1
                //dialogMaskBgColor : "#000", // 设置透明遮罩层的背景颜色，全局通用，默认为#fff
                //imageUpload: true,
                //imageFormats: ["jpg", "jpeg", "gif", "png", "bmp", "webp"],
                //imageUploadURL: "./php/upload.php",
                onload: function ()
                {
                    console.log('onload', this);
                    //this.fullscreen();
                    //this.unwatch();
                    //this.watch().fullscreen();

                    //this.setMarkdown("#PHP");
                    //this.width("100%");
                    //this.height(800);
                    //this.resize("100%", 640);
                }
            });
        });
    },
    submit: function ()
    {
        var title = $('#title');
        var code_type = $('#code-type');
        var post_type = $('#post-type');
        $('#submit').click(function ()
        {
            if (title.val())
            {
                if (code_type.val())
                {
                    if (post_type.val())
                    {
                        $(this).html("提交中……");
                        $.post("/code/release/submitrelase", {
                            title: $('#title').val(),
                            codetype: $('#code-type').val(),
                            tags: $('#post-type').val(),
                            md: testEditor.getMarkdown(),
                            html: testEditor.getHTML()
                        }, function (data)
                        {
                            var type = data.split("=");

                            if (type[0] < 0)
                                toastr.error(type[1]);
                            else
                                toastr.success(type[1]);
                        });
                        $(this).html("发布文章");
                    } else
                        toastr.error("请选择文章类型！");
                } else
                    toastr.error("请选择代码类型！");
            } else
                toastr.error("请填写文章标题");
        });
    },
    left_btn: function (username, userrealname)
    {
        var isClick = false;
        $('#left-menu-btn').click(function ()
        {
            var left_menu = $('#left-menu');
            var left_menu_insert = $('#left-menu-insert');
            var left_menu_close = $('#left-menu-close');
            var left_menu_headimg = $('#left-menu-headimg');
            var left_menu_userinfo = $('#left-menu-userinfo');
            if (!isClick)
            {
                left_menu.animate({
                    width: 300
                }, function ()
                {
                    left_menu_insert.find('span').html("   代码插入工具");
                    var html = '<p style="color: white">' + username + '</p>';
                    html += '<p style="color: white">' + userrealname + '</p>';
                    left_menu_userinfo.html(html);
                });
                left_menu_headimg.animate({
                    width: 100,
                    height: 100
                });
                isClick = true;
            }
            else
            {
                left_menu.animate({
                    width: 45
                });
                left_menu_headimg.animate({
                    width: 30,
                    height: 30
                });
                left_menu_insert.find('span').html("");
                left_menu_userinfo.html("");
                isClick = false;
            }
        });

        $('#left-menu-close').click(function ()
        {
            $('#left-menu').fadeOut(1000);
            $('#left-float').fadeIn(1300);
        });

        $('#left-float').click(function ()
        {
            $('#left-menu').fadeIn(1000);
            $('#left-float').fadeOut(1300);
        });
    }
};